const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    selected: false,
    choseNames: [],
    nowTime: getApp().globalData.nowTime,
    nameCall: '精确搜索',
    nameColor: false,
    nameFlag: false,
    info:'',
  },

  getInputValue: function(e){
    this.setData({
      info: e.detail.value
    })
  },
  selectData: function(){
    this.alreadyShow();
  },

  comfirmsel: function(e) {
    const list = this.data.list;
    const allList = [];
    for (let i = 0; i < list.length; i++) {
      if (list[i].Checked) {
        if (list[i].datetime1 != '') {
          let data_all = {};
          data_all.docEntry = list[i].DocEntry;
          data_all.lineNum = list[i].LineNum;
          data_all.itemCode = list[i].ItemCode;
          data_all.itemName = list[i].ItemName;
          data_all.u_Spec = list[i].U_Spec;
          data_all.quantity = list[i].Quantity;
          data_all.u_WetQty = list[i].U_WetQty;
          data_all.datetime1 = list[i].datetime1;
          allList.push(data_all);
        } else {
          wx.showToast({
            title: '打勾后未填写回复交期！编号：' + list[i].U_OrderNum,
            icon: 'none',
            duration: 2000
          })
          return
        }
      } 
    }
    // console.log(allList)
    // var Logo=JSON.stringify(allList)
    if (allList.length > 0) {
      this.submit(allList);
    } else {
      wx.showToast({
        title: '未勾选提交信息',
        icon: 'error',
        duration: 2000
      })
    }
  },

  submit: function(Logo){
    console.log(Logo)
    var that = this;
    wx.request({
      url: globalIp + 'home/oitm/insertSCFFShip',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
        cardCode: wx.getStorageSync('userInfo').CardCode || '',
        userName: wx.getStorageSync('userInfo').trueName || '',
        purShip: Logo
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          that.setData({
            choseNames: []
          })
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: () => {
        that.onShow();
      }
    })
  },

  changeDate1: function(e) {
    console.log(e)
    const list = this.data.list;
    const line = parseInt(e.currentTarget.dataset.number)-1;
    if (list[line].Checked) {
      list[line].datetime1 = e.detail.value;
      this.setData({
        list
      })
    } else {
      wx.showToast({
        title: '打勾后再填日期',
        icon: 'error',
        duration: 2000
      })
    }
  },

  checkboxChange: function(e) {
    const list = this.data.list;
    const values = e.detail.value;
    for (let i = 1; i <= list.length; i++) {
      if (values.indexOf(i.toString())>=0) {
        list[i-1].Checked = true;
      } else {
        list[i-1].Checked = false;
      }
    }
    this.setData({
      list,
      choseNames: e.detail.value, //单个选中的值
    })
  },

  checkboxAll: function(){
    const list = this.data.list;
    const name = [];
    for (let i = 0; i < list.length; i++) {
      list[i].Checked = !this.data.selected
      if (!this.data.selected) {
        name.push(list[i].rownumber.toString())
      }
    }
    this.setData({
      list,
      selected: !this.data.selected,
      choseNames: name
    })
  },

  alreadyShow: function(){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/oitm/selPurUserSCFFShip',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
        cardCode: wx.getStorageSync('userInfo').CardCode || '',
        opinion: that.data.info
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          list: res.data.jsonObject.data
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.alreadyShow();
  },

})